using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;


using student_factory.DAO;
using student_factory.Metier;

namespace student_factory.EnregistrementCommande
{
    public partial class EnregistrementCommande : Form
    {
        private IList<ContenuProjet> profils = new List<ContenuProjet>();
        private IList<string> id_clients = new List<string>();

        public EnregistrementCommande()
        {
            InitializeComponent();
            comboBoxClient.Items.Add("");
            foreach (Client client in ClientDAO.getClients()){
                comboBoxClient.Items.Add(client.Cli_raison_social);
                id_clients.Add( client.Id_c );
            }

            foreach (Profil profil in ProfilDAO.getProfils())
            {
                ContenuProjet p = new ContenuProjet();
                p.Nom = profil.Pro_nom;
                p.Id = profil.Id_i;
                p.Quantite = 0;

                this.profils.Add( p );
                listBoxProfilesListe.Items.Add(profil.Pro_nom.ToString() + " - " + 0 );
                comboBoxProfiles.Items.Add(profil.Pro_nom.ToString());
            }
        }

        private void buttonProfileAjouter_Click(object sender, EventArgs e)
        {
            int temp;
            if (Int32.TryParse(textBoxProfilesNombre.Text,out temp) && comboBoxProfiles.Text != "")
            {

                listBoxProfilesListe.Items.Clear();
                foreach (ContenuProjet profil in this.profils)
                {
                    if (profil.Nom.Equals(comboBoxProfiles.Text))
                    {
                        profil.Quantite = temp;
                        
                    }
                    listBoxProfilesListe.Items.Add(profil.Nom + " - " + profil.Quantite);
                }
            }
        }

        private void buttonProjetCreer_Click(object sender, EventArgs e)
        {
            string id_c = "";
            string id_p = "";
            string id_u = "{00D79485-F52C-4A6F-8AF8-0718608D874F}";

            // test nouveau client
            if ( comboBoxClient.Text != "" )
            {
                // pas un nouveau
                id_c = id_clients[ comboBoxClient.SelectedIndex ];
            }
            else if ( textBoxClient.Text != "" )
            {
                // nouveau
                ClientDAO.addClient(textBoxClient.Text,out id_c);
            }

            if (textBoxProjetNom.Text != "")
            {
                
                ProjetDAO.addProjet( id_c , id_u , textBoxProjetNom.Text, textBoxProjetDescription.Text, out id_p);
                foreach (ContenuProjet profil in this.profils)
                {
                    Console.WriteLine(id_p + "   " + profil.Id);
                    ProjetDAO.addContenuProjet(id_p , profil.Id , profil.Quantite);
                }

                Dispose();

            }

        }
    }
}